/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo;

import VO.GuardiaVO;
import java.sql.SQLException;
import utiles.exceptions.DataBaseException;
import utiles.exceptions.InternalErrorException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Vector;
import javax.sql.DataSource;
import utiles.SQL.GeneralOperations;
import utiles.SQL.SimpleDataSource;

/**
 *
 * @author Jorge
 */
public class DaoGuardia implements DaoGuardiaInterface{

    public boolean registrarGuardia(GuardiaVO nuevo) throws DataBaseException, InternalErrorException, SQLException {
         Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1;




            


            for (int i = 0; i < nuevo.getVoluntarios().size(); i++) {

                queryString1 = "CALL registrarGuardia(?,?)";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                callabledStatement.setString("fechap", nuevo.getFecha());
                callabledStatement.setString("rutp", nuevo.getVoluntarios().get(i).toString());


                callabledStatement.execute();



            }





            if (callabledStatement.getUpdateCount() > 0) {
                result = true;
            }

        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public Vector cargarGuardias() throws DataBaseException, InternalErrorException, SQLException {
         Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        Vector result = null;
        GuardiaVO aux;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarGuardias()";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);

            callabledStatement.execute();

            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst() != false) {
                result = new Vector();

                while (resultSet.next()) {
                    aux = new GuardiaVO();

                    aux.setFecha(resultSet.getString("fecha"));
                    aux.setVoluntario(resultSet.getString("nombre"));

                    result.add(aux);
                }



            }


        } catch (SQLException e) {
            result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }

    }

}
